package com.beyondprototype.batch.config;

import java.util.List;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;

import com.beyondprototype.batch.model.CustomerMarker;
import com.beyondprototype.batch.service.CustomerMarkerService;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class IhubDataBatchConfiguration {
    
    private String ihubhkDataFile = "/Users/xwp/Desktop/cngit/springbatch/usecase/ihubhk-data.txt";
    private String ihubsgDataFile = "/Users/xwp/Desktop/cngit/springbatch/usecase/ihubsg-data.txt";

    @Autowired
    private CustomerMarkerService customerMarkerService;

    @Bean
    @Qualifier("ihubhkItemReader")
    public ItemReader<CustomerMarker> ihubhkItemReader() {
        FlatFileItemReader<CustomerMarker> reader = new FlatFileItemReader<CustomerMarker>();
        //reader.setResource(new ClassPathResource(gcduDataFile));
        reader.setResource(new FileSystemResource(ihubhkDataFile));
        reader.setLineMapper(new DefaultLineMapper<CustomerMarker>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] {"asOfDate","localIdentifer","market","globalIdentifier","markers"});
            }});

            setFieldSetMapper(new BeanWrapperFieldSetMapper<CustomerMarker>() {{
                setTargetType(CustomerMarker.class);
            }});
        }});
        return reader;
    }
    
    //insert customer linkage records into mongodb
    @Bean 
    @Qualifier("ihubhkItemWriter")
    public ItemWriter<CustomerMarker> ihubhkItemWriter() {
        return new ItemWriter<CustomerMarker>() {
            @Override
            public void write(List<? extends CustomerMarker> items) throws Exception {
                for (CustomerMarker item : items) {
                    log.info("Updating "+ item.toString());
                }
                
                customerMarkerService.update(items);
            }
        };
    }

    @Bean
    @Qualifier("ihubhkItemProcessor")
    public ItemProcessor<CustomerMarker, CustomerMarker> ihubhkItemProcessor() {
        return new ItemProcessor<CustomerMarker, CustomerMarker>() {
            @Override
            public CustomerMarker process(CustomerMarker item) throws Exception {
            	
            	log.info("Processing "+item.toString());
            	
            	item.setStatus("Updated");
            	
                return item;
            }
        };
    }
}
